﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace AnguaRPGEngine.DND4eModule
{
    /// <summary>
    /// Class for importing characters created using the character builder
    /// </summary>
    public class CharacterBuilderImporter
    {
        private XmlDocument _Doc;
        public DNDPlayerCharacter Import(string FileSpec)
        {
            DNDPlayerCharacter pc = new DNDPlayerCharacter();
            TextReader reader = File.OpenText(FileSpec);
            string FileBody = reader.ReadToEnd();
            reader.Close();
            _Doc = new XmlDocument();
            _Doc.LoadXml(FileBody);
            pc.Initialize();
            pc.CharacterName = GetNodeText("D20Character/CharacterSheet/Details/name");
           // pc.Level = int.Parse(GetNodeText("D20Character/CharacterSheet/Details/Level")); // Read Only, based on XP
            pc.PlayerName = GetNodeText("D20Character/CharacterSheet/Details/Player");
            pc.Height = GetNodeText("D20Character/CharacterSheet/Details/Height");
            pc.Weight = GetNodeText("D20Character/CharacterSheet/Details/Weight");
            pc.Gender = GetNodeText("D20Character/CharacterSheet/Details/Gender");
            pc.Age = int.Parse("0" + GetNodeText("D20Character/CharacterSheet/Details/Age"));
            pc.AlignmentFromString(GetNodeText("D20Character/CharacterSheet/Details/Alignment"));
            pc.AdventuringCompanyOrOtherAffiliations = GetNodeText("D20Character/CharacterSheet/Details/Company");
            pc.XP = int.Parse("0" + GetNodeText("D20Character/CharacterSheet/Details/Experience"));

            return pc;
        }
        private string GetNodeText(string Path)
        {
            XmlNode node = _Doc.SelectSingleNode(Path);
            return node.InnerText.Replace("&apos;","'").Trim();
        }
    }
}
